#include "APP.h"
#include "crc8.h"
#include "ctrl_init.h"
#include "bsp_dma.h"
#include "bsp_can.h"
uint16_t NUVN_state = 0x00;
uint16_t NUVN_error = 0x00;
reive_data_t reive_data;
APP_t APP;
static uint8_t finish_flag = 0;
static uint8_t Send_Pro = 0x00;

uint8_t GetFinshFlag(void)
{
	return finish_flag;
}

void SetFinishFlag(uint8_t num)
{
	finish_flag = num;
}


extern uint8_t Horn_crtl;

uint8_t crc8_test[2] = {0x00};
void server_send_to_master(uint8_t *buff)
{
	crc8_test[0] = buff[12];
	crc8_test[1] = CRC8_Table(buff,12);
	if(buff[0] == 0xF1 && buff[1] == 0xE2 && buff[2] == 0x0E && buff[12] == CRC8_Table(buff,12))
	{
		reive_data.mode = buff[3];
		reive_data.pro  = buff[4];
		reive_data.year = buff[5];
		reive_data.month = buff[6];
		reive_data.day = buff[7];
		reive_data.hour = buff[8];
		reive_data.minute = buff[9];
		reive_data.second = buff[10];
		reive_data.millisecond = buff[11];
	}
}



uint8_t Get_Pro(void)
{
	return reive_data.pro;
}

void Clear_Pro(void)
{
	reive_data.pro = 0x00;
}






void master_send_to_ctrl(uint32_t CAN_ID)
{
	static uint8_t buff[8] = {0};
	buff[0] = 0xAA;
	buff[1] = reive_data.pro;
	CAN2_TX_PACKET(CAN_ID,buff,8);
}

uint8_t GetReiveProData(void)
{
	return reive_data.pro;
}

uint16_t NUVN_Error = 0x0000;

uint8_t Ax_State[4] = {0x00,0x00,0x00,0x00};
uint8_t AXIS_Temp[2] = {0};
uint8_t AXIS_Hump[2] = {0};

WeatherData_t WeatherData;

void master_send_to_server_data(void)
{
	static uint8_t server_data[23] = {0};
	
	uint16_t num = 0;
	
	server_data[0]  = 0xF1;
	server_data[1]  = 0xE1;
	server_data[2]  = 0x17;
	
	
	num = CtrlPm.Weather.Data.Temp * 10;
	server_data[3]  = num >> 8;
	server_data[4]  = num;
	
	num = CtrlPm.Weather.Data.Hum * 10;
	server_data[5]  = num >> 8;
	server_data[6]  = num;
	
	num = CtrlPm.Weather.Data.Wind.speed * 10;
	server_data[7]  = num >> 8;
	server_data[8]  = num;
	
	num = CtrlPm.Weather.Data.Wind.dir * 10;
	server_data[9]  = num >> 8;
	server_data[10]  = num;
	
	
	server_data[11] = AXIS_Temp[0];
	server_data[12] = AXIS_Temp[1];
	server_data[13] = AXIS_Hump[0];
	server_data[14] = AXIS_Hump[1];
	server_data[15] = NUVN_Error << 8;
	server_data[16] = NUVN_Error;
	server_data[17] = Ax_State[remote_state];
	server_data[18] = Ax_State[door_state];
	server_data[19] = Ax_State[charge_state];
	server_data[20] = Ax_State[claw_state];
	server_data[21] = CRC8_Table(server_data,21);
	server_data[22] = 0x19;
//	send_data_dma_u2(server_data,23);
}



void Send_Server_pro(uint8_t pro)
{
	Send_Pro = pro;
}

uint8_t Get_Server_Pro(void)
{
	return Send_Pro;
}
void master_send_to_server_answer(void)
{
	static uint8_t answer[15] = {0};
	answer[0] = 0xF1;
	answer[1] = 0xE3;
	answer[2] = 0x0F;
	answer[3] = reive_data.mode;
	answer[4] = Get_Server_Pro();
	answer[5] = GetFinshFlag();
	answer[6] = reive_data.year;
	answer[7] = reive_data.month;
	answer[8] = reive_data.day;
	answer[9] = reive_data.hour;
	answer[10] = reive_data.minute;
	answer[11] = reive_data.second;
	answer[12] = reive_data.millisecond;
	answer[13] = CRC8_Table(answer,13);
	answer[14] = 0x19;
////send_data_dma_u2(answer,15);
}



